<?php
namespace app\admin\controller\nursinguser;;

use app\admin\model\NewsMessage;
use app\admin\model\StaffPeople;
use app\admin\model\User;
use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use app\admin\model\publicuse\PublicUse;
use smartybuilder\libs\Quickbuilder;
use think\App;
/**
 * @ControllerAnnotation(title="护理订单")
 * Class Orders
 */
class Orders extends AdminController
{
    use \app\admin\traits\Curd;
     protected $allowModifyFields = [
        'is_live',
        'is_assessment',
        'status',
    ];
    protected $noExportFields = [
        'id',
        'name',
        'order_num',
        'sort',
        'nursing_lang_id',
        'cover_pepo_name',
        'cover_pepo_sex',
        'is_live',
        'phone',
        'province',
        'city',
        'address',
        'assessment_time',
        'purchase_time',
        'service_time',
        'days',
        'money',
        'staff_people_id',
        'area',
        'uid',
        'age',
        'pay_state',
        'service_money',
        'platform_money',
        'end_time',
        'contact_name',
        'is_assessment',
        'state',
        'create_time',
        'update_time',
        'status',
        'delete_time',
    ];
    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new \app\admin\model\NursingOrder();
        $array = PublicUse::getConfigDir(__CLASS__);
        if(is_file(__DIR__.DIRECTORY_SEPARATOR.'field'.DIRECTORY_SEPARATOR.$array['class'].'.txt')){
           $this->AllData = json_decode(file_get_contents(__DIR__.DIRECTORY_SEPARATOR.'field'.DIRECTORY_SEPARATOR.$array['class'].'.txt'),true);
        }
    }
    public function index()
    {
        $param = request()->param();
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames([],$this->AllData['field']??[]);
            if(isset($this->AllData['is_page'])&&$this->AllData['is_page']==0){
                $limit = 10000;
            }
            $where = PublicUse::getParameter($where);
//            $where[] = ['id','=',16];
            $count = $this->model
                ->where($where)
                ->count();
            try{
                $list = $this->model
                    ->where($where)
                    ->page($page, $limit)
                    ->order($this->sort)
                    ->select();
            }catch (\Exception $e){
                $list = $this->model
                    ->where($where)
                    ->page($page, $limit)
//                ->order($this->sort)
                    ->select();
            }
//            dump($list->toArray());die();
            $list = PublicUse::objctToModelAndField($list,$this->AllData['field']??[],true,['staff_people_id'=>function($field){
                $user = StaffPeople::where('id',$field)
                    ->field('id,name,phone')
                    ->findOrEmpty()->toArray();
//                echo '<pre>';
//                print_r($user);
//                exit;
                if(empty($user)){
                    return '';
                }else{
                    if(!empty($user['phone'])){
                        return $user['name']."({$user['phone']})";
                    }else{
                        return $user['name'];
                    }
                }
            }]);
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        $parameter = '';
        $fi = $this->model->getPk();
        if(!empty($param['field'])&&!empty($param[$fi])){
            $parameter = "?{$param['field']}={$param[$fi]}";
        }
        $newwhere = $this->request->get();
        foreach ($newwhere as $key=>$value){
            if($key!==$fi&&$key!=='field'){
                if(strpos($parameter,'?')===false){
                    $parameter .= "?{$key}={$value}";
                }else{
                    $parameter .= "&{$key}={$value}";
                }
            }
        }
//        echo $parameter;exit;
        $this->assign('parameter',$parameter);
        return $this->fetch();
    }
    /**
     * @NodeAnotation(title="安排员工")
     */
    public function anpai(){
        $id = $this->request->param('id');
        if(empty($id)){
            return $this->error('请输入要添加的id');
        }
        $data = $this->request->param();
        $shuju = $this->model->find($id);
        if(isset($data['phone'])){
            $user = StaffPeople::where('phone',$data['phone'])
                ->find();
            if(empty($user)){
                return $this->error('员工没有查询到');
            }
            $shuju->staff_people_id = $user->id;
            $bool = $shuju->save(['update_time'=>time()]);
            if(!$bool){
                return $this->error('配置失败');
            }
            $news_message = [
                'name'=>'你有一条接单信息',
                'uid'=>$user->id,
                'content'=>'公司为你安排了一个订单，请进入订单界面接取',
                'type'=>2,
                'href'=>'',
                'read'=>0,
                'status'=>1,
                'update_time'=>time(),
                'create_time'=>time(),
            ];
            NewsMessage::create($news_message);
            return $this->success('配置成功');
        }else{
            $user = StaffPeople::where('id',$shuju->getData('staff_people_id'))
                ->find();
            return Quickbuilder::makeForm()
                ->setAction()
                ->set_up_form('none','','id',$id)
                ->set_up_form('input','员工手机号码','phone',empty($user->phone)?'':$user->phone)
                ->set_button('submit','提交')
                ->fetch();
        }
    }
}